
local indata LAD\Data
local infile LAD\Data_Cleaning

use "`indata'\LAD_Crowdout.dta", clear

do "`infile'\LAD_Commands_filtering.do"

*Non-Unionized RPP Non-Members do not satisfy the Test of Running Variable and are excluded
keep if dues>0
*Focus on RPP members with strictly positive RRSP contributions
keep if penadj>0 & rspcont>0

*Need to respecify the kink point based on the randomization, so drop existing variables
drop kink empinc_kink
*Readjust the employment income variable since this was changed when "Commands_filtering.do" was executed
replace empinc=empinc+cqppympe

local covars age agesq female married province_* selfempinc_flag eiinc_flag othinc disab medexp

*Initiate the permutation loop for 1,000 iterations
foreach i of numlist 1/1000 {	
	preserve
	
	*Randomly set the new policy threshold to be between 0.95 to 1.35 times its current value based on a uniform distribution
	gen permut=0.95+(1.35-0.95)*runiform() if _n==1
	summarize permut
	
	replace cqppympe=cqppympe*permut[1]
	*Now, readjust earnings and set the kink point based on the randomly chosen policy threshold
	replace empinc=empinc-cqppympe
	gen kink=(empinc>=0)
	gen empinc_kink=empinc*kink
	
	*Now, run the regressions and obtain the crowd-out statistic using the preferred bandwidth (now centred differently)
	keep if empinc>=-6000 & empinc<6000
	qui reg penadj empinc empinc_kink `covars' if rspcont<rspdlc & (penadjl+rspcont)<contlimit
	est store rppreg
	qui reg rspcont empinc empinc_kink `covars' if rspcont<rspdlc & (penadjl+rspcont)<contlimit
	est store savreg
	
	qui suest savreg rppreg, cl(id)
	nlcom [savreg_mean]_b[empinc_kink]/[rppreg_mean]_b[empinc_kink]

	restore
}

*Run the regression with the actual policy threshold
replace empinc=empinc-cqppympe
gen kink=(empinc>=0)
gen empinc_kink=empinc*kink
	
keep if empinc>=-6000 & empinc<6000
qui reg penadj empinc empinc_kink `covars' if rspcont<rspdlc & (penadjl+rspcont)<contlimit
est store rppreg
qui reg rspcont empinc empinc_kink `covars' if rspcont<rspdlc & (penadjl+rspcont)<contlimit
est store savreg
	
qui suest savreg rppreg, cl(id)
nlcom [savreg_mean]_b[empinc_kink]/[rppreg_mean]_b[empinc_kink]

exit
